﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MT.ImageTool.GaoDeInfrared
{
    struct IptPictureFrame
    {

    };

    public class GaoDeInfraredImageHelper
    {
        public GaoDeInfraredImageHelper()
        {

        }


        /// <summary>
        ///  DLT-664格式热图
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public static GaoDeImageJPGModel? GetGaoDeImageJPGModel(string imagePath)
        {
            GaoDeImageJPGModel imageJPGModel = new GaoDeImageJPGModel();
            byte[] expectedFileEndType = { 0x37, 0x66, 0x07, 0x1a, 0x12, 0x3a, 0x4c, 0x9f, 0xa9, 0x5d, 0x21, 0xd2, 0xda, 0x7d, 0x26, 0xbc };

            try
            {
                // 创建一个FileStream对象，用于读取文件  
                using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
                {
                    // 创建一个BinaryReader对象，用于从FileStream中读取二进制数据  
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        // 读取文件的所有内容为一个字节数组  
                        byte[] imageBytes = br.ReadBytes((int)fs.Length);
                        int nLenTotal = imageBytes.Length;

                        //读取文件结尾，并判断文件结尾是否相同
                        const int endTypeLen = 16;
                        byte[] endType = new byte[endTypeLen];
                        Array.Copy(imageBytes, nLenTotal - endTypeLen, endType, 0, endType.Length);
                        if (!endType.SequenceEqual(expectedFileEndType))
                        {
                            MessageBox.Show("文件结尾格式不正确！", "提示");
                            return null;
                        }

                        //读取红外数据的偏移地址
                        const int iROffsetLen = 4;
                        byte[] iRoffsetLen = new byte[iROffsetLen];
                        Array.Copy(imageBytes, nLenTotal - endTypeLen - iROffsetLen, iRoffsetLen, 0, iRoffsetLen.Length);
                        int nIRDataOffsetAddr = BitConverter.ToInt32(iRoffsetLen, 0);
                        imageJPGModel.IROffsetAddr = nIRDataOffsetAddr;

                        //文件版本号
                        const int iVerLen = 2;
                        byte[] verInfo = new byte[iVerLen];
                        Array.Copy(imageBytes, nIRDataOffsetAddr, verInfo, 0, verInfo.Length);

                        //文件宽度
                        const int iWidthLen = 2;
                        byte[] widthBytes = new byte[iWidthLen];
                        Array.Copy(imageBytes, nIRDataOffsetAddr + iVerLen, widthBytes, 0, widthBytes.Length);
                        short width = BitConverter.ToInt16(widthBytes, 0);
                        imageJPGModel.ImageWidth = width;

                        //文件高度
                        const int iHeightLen = 2;
                        byte[] heightBytes = new byte[iHeightLen];
                        Array.Copy(imageBytes, nIRDataOffsetAddr + iVerLen + iWidthLen, heightBytes, 0, heightBytes.Length);
                        short height = BitConverter.ToInt16(heightBytes, 0);
                        imageJPGModel.ImageHeight = height;

                        //拍照时间
                        const int iShootDateLen = 14;
                        byte[] shootDateBytes = new byte[iShootDateLen];
                        Array.Copy(imageBytes, nIRDataOffsetAddr + iVerLen + iWidthLen + iHeightLen, shootDateBytes, 0, shootDateBytes.Length);
                        string shootDateStr = Encoding.ASCII.GetString(shootDateBytes);
                        imageJPGModel.ShootDateStr = shootDateStr;

                        //温度矩阵
                        int iTemperLen = width * height * sizeof(float);
                        byte[] temperBytes = new byte[iTemperLen];
                        Array.Copy(imageBytes, nIRDataOffsetAddr + iVerLen + iWidthLen + iHeightLen + iShootDateLen, temperBytes, 0, temperBytes.Length);

                        float[] temperFloats = new float[iTemperLen / sizeof(float)];
                        for (int i = 0; i < temperFloats.Length; i++)
                        {
                            temperFloats[i] = BitConverter.ToSingle(temperBytes, i * 4);
                        }
                        imageJPGModel.TemperFloat = temperFloats.ToList();

                        float maxTemper = temperFloats.Max(x => x);
                        float minTemper = temperFloats.Min(x => x);
                        float avgTerper = temperFloats.Average(x => x);

                        imageJPGModel.MaxTemper = maxTemper;
                        imageJPGModel.MinTemper = minTemper;
                        imageJPGModel.AvgTemper = avgTerper;
                    }
                }

                return imageJPGModel;
            }
            catch (Exception ex)
            {
                // 捕获并处理任何可能出现的异常  
                Console.WriteLine("读取JPG文件时发生错误: " + ex.Message);
                return null;
            }
        }
    }
}
